package com.leo.apptest.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.leo.apptest.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangguobao on 2017/2/6.
 */
public class AnimRecyclerAdapter extends RecyclerView.Adapter {
    private List<String> mAnimStrings;

    public AnimRecyclerAdapter() {
        mAnimStrings = new ArrayList<>();
    }

    public void setList(List<String> animStrings) {
        mAnimStrings = animStrings;
        notifyItemRangeInserted(0, mAnimStrings.size() - 1);
    }

    public void removeData(){
        mAnimStrings.remove(0);
        notifyItemRemoved(0);
    }

    public void addFirstData(String data) {
        if (mAnimStrings.isEmpty()) {
            mAnimStrings.add(data);
        } else {
            mAnimStrings.add(0, data);
        }
        notifyItemInserted(0);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new TestHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_test_recycler, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        TestHolder viewHolder = (TestHolder) holder;
        String s = mAnimStrings.get(position);
        viewHolder.mTvTest.setText(s);
    }

    @Override
    public int getItemCount() {
        return mAnimStrings.size();
    }

    private class TestHolder extends RecyclerView.ViewHolder {

        private final TextView mTvTest;

        public TestHolder(View view) {
            super(view);
            mTvTest = (TextView) view.findViewById(R.id.tvTest);
        }
    }
}
